package com.utcs.common.persistence;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.utcs.common.utils.StringUtils;
import com.utcs.modules.resource.basic.entity.User;
import com.utcs.modules.resource.basic.utils.UserUtils;
import org.hibernate.validator.constraints.Length;

import java.util.Date;

/**
 * Created by Fly on 2017/5/23.
 */
public class DataEntity<T> extends BaseEntity<T> {

    protected User createBy;//创建者
    protected Date createDate;//创建时间
    protected User updateBy;//更新者
    protected Date updateDate;//更新时间
    protected String description;//描述
    protected String showSign;//显示标志


    /**
     * 显示标记（1：正常；0：不显示）
     */
    public static final String SHOW_SIGN_VISIBLE = "1";
    public static final String SHOW_SIGN_INVISIBLE = "0";

    public DataEntity() {
        super();
        this.showSign = SHOW_SIGN_VISIBLE;
    }

    public DataEntity(Integer id) {
        super(id);
    }

    /**
     * 插入之前执行方法，需手动调用
     */
    @Override
    public void preInsert() {
        User user = UserUtils.getUser();
        if (user.getId() != null) {
            this.updateBy = user;
            this.createBy = user;
        }
        this.updateDate = new Date();
        this.createDate = this.updateDate;
    }

    /**
     * 更新之前执行方法，需手动调用
     */
    @Override
    public void preUpdate() {
        User user = UserUtils.getUser();
        if (user.getId() != null) {
            this.updateBy = user;
        }
        this.updateDate = new Date();
    }


    @JsonIgnore
    public User getCreateBy() {
        return createBy;
    }

    public void setCreateBy(User createBy) {
        this.createBy = createBy;
    }

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    @JsonIgnore
    public User getUpdateBy() {
        return updateBy;
    }

    public void setUpdateBy(User updateBy) {
        this.updateBy = updateBy;
    }

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    public Date getUpdateDate() {
        return updateDate;
    }

    public void setUpdateDate(Date updateDate) {
        this.updateDate = updateDate;
    }

    @Length(max = 255)
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @JsonIgnore
    @Length(max = 1)
    public String getShowSign() {
        return showSign;
    }

    public void setShowSign(String showSign) {
        this.showSign = showSign;
    }
}
